package com.cxq.finalproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.cxq.finalproject.ShowDetailActivity;
import com.cxq.finalproject.entity.Item;
import com.cxq.finalproject.R;

import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<MyViewHolder>  {

    private LayoutInflater mInflate;
    private Context mContext;
    private List<Item> mData;

    public RecyclerAdapter(Context context, LayoutInflater mInflate, List<Item> mData) {
        this.mContext=context;
        this.mInflate = mInflate;
        this.mData = mData;
    }

    /**
     *
     * @param viewGroup 父布局 RecyclerView
     * @param i 相当于position
     * @return ViewHolder
     */
    @Override
    public  MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        // 不需要检查是否复用，因为只要进入此方法，必然没有复用
         // 因为RecyclerView 通过Holder检查复用
        View view = mInflate.inflate(R.layout.home_items,viewGroup,false);
         MyViewHolder myViewHolder = new  MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder( MyViewHolder myViewHolder, int position   ) {
        myViewHolder.tv.setText(mData.get(position).getTitle());
        myViewHolder.mImageview.setImageResource(mData.get(position).getImg());
        myViewHolder.tvprice.setText(mData.get(position).getPrice());
        myViewHolder.itemView.setBackgroundColor(Color.WHITE);
        myViewHolder.itemView.setBackgroundResource(R.drawable.button_circle_shape);
        myViewHolder.imgSrc = mData.get(position).getSrcName();
        if(mData.get(position).getTitle().equals("女装")){
            myViewHolder.tv.setBackgroundColor(0xFFFF0000);
        }else if(mData.get(position).getTitle().equals("男装")){
            myViewHolder.tv.setBackgroundColor(0xFF00A5FF);
        }else{
            myViewHolder.tv.setBackgroundColor(0xFFAAB220);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
    //添加数据
    public void addItem(Item data, int position) {
        mData.add(position, data);
        notifyItemInserted(position);
    }
    //删除数据
    public void removeItem(Item data) {
        int position = mData.indexOf(data);
        mData.remove(position);
        notifyItemRemoved(position);
    }
}

/**
 * MyViewHolder中获得子布局中的view
 */
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    TextView tv;
    TextView tvprice;
    ImageView mImageview;
    String imgSrc;
    public MyViewHolder(View itemView) {
        super(itemView);
        // 通常ViewHolder的构造，就是用于获取控件视图的
        tv= (TextView) itemView.findViewById(R.id.textView8);
        mImageview= (ImageView) itemView.findViewById(R.id.main_item_imageView);
        tvprice=(TextView) itemView.findViewById(R.id.main_item_price);
        // TODO 后续处理点击事件的操作
        mImageview.setOnClickListener(this);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = getAdapterPosition();
                Context context = mImageview.getContext();
                //intent中传入一个参数：imgSrc
                Intent intent = new Intent(context, ShowDetailActivity.class);
                intent.putExtra("imgSrc",imgSrc);
                context.startActivity(intent);
            }
        });

        tvprice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = getAdapterPosition();
                Context context = mImageview.getContext();
                //intent中传入一个参数：imgSrc
                Intent intent = new Intent(context, ShowDetailActivity.class);
                intent.putExtra("imgSrc",imgSrc);
                context.startActivity(intent);
            }
        });
    }
    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        Context context = mImageview.getContext();
        //intent中传入一个参数：imgSrc
        Intent intent = new Intent(context, ShowDetailActivity.class);
        intent.putExtra("imgSrc",imgSrc);
        context.startActivity(intent);
    }
}